# -*- coding: utf-8 -*-
 ##############################################
 ##                                            ##
 ##                Basiq Empresa                ##
 ##                                             ##
 ##                                             ##
 ##              from Basiq Series              ##
 ##           by Críptidos Digitales            ##
 ##                 GPL (c)2008                 ##
  ##                                            ##
    ##############################################

""" Sistema Basiq Empresa
    Control de Personas, Productos y Procesos en una empresa pequeña.
"""

import sys
from PyQt4 import QtCore, QtGui

import configuracion

import baseController

import access
import propio



# import locale
# locale.setlocale(locale.LC_ALL, es_MX)


class MyApp(QtGui.QApplication):

    BASICO = 0
    TALLER = 1

    content = u'Básico'

    def __init__(self,  *args):
        QtGui.QApplication.__init__(self, *args)

        self.__screenGeometry = QtGui.QDesktopWidget().screenGeometry()
        self.__reach = 128
        self.__controller = {}

    @property
    def distribution(self):
        """property 'distribution', read only, class"""
        return self.BASICO

    @property
    def fontZoom(self):
        if self.__screenGeometry.height() == 1024:
            return 1.6
        elif self.__screenGeometry.height() == 800:
            return 1.25
        elif self.__screenGeometry.height() <= 768:
            return 1.20
        else:
            return 1.0

    @property
    def info(self):
        """property 'info', read only, dict"""
        return {'version':u"0.2.0", 'title':u"Basiq Empresa", 'author':u"Jorge Hojyo", 'company':u"Críptidos Digitales", 'license':u"GPL (c) 2008", 'alias':u"bE"}

    @property
    def reach(self):
        return self.__reach

    @property
    def screenGeometry(self):
        return self.__screenGeometry

    def setReach(self, valor):
        self.__reach = valor


if __name__ == "__main__":

    app = MyApp(sys.argv)
    app.setQuitOnLastWindowClosed(True)

    session = baseController.Session(app=app)

    session.config = configuracion.Parser(filename="%s.cfg" % app.info['alias'])

    try:
        session.config.pull('layout', 'accessWindowState')
    except:
        print sys.exc_info()
        QtGui.QMessageBox.critical(None, app.info['title'], u"El archivo de configuracion TB.cfg contiene errores\n\n\nComunícate con el administrador del sistema", QtGui.QMessageBox.Ok)
    else:

        access.session = session
        formaAcceso = access.Form()
        result = formaAcceso.exec_()

        if result == QtGui.QDialog.Accepted:
            formaAcceso.destroy()

            session.initDb()

            if session.isFirstRun():
                formaDatos = propio.Forma(app=app)
                formaDatos.agrega()
                result = formaDatos.exec_()

            if result == QtGui.QDialog.Accepted:

                session.incrementRunCount()

                splash = QtGui.QSplashScreen(QtGui.QPixmap("./imagenes/logo.png"), QtCore.Qt.WindowStaysOnTopHint)
                splash.showMessage(u"Cargando %s %s ..." % (app.info['title'], app.info['version']), QtCore.Qt.AlignBottom, QtGui.QColor("#4040FF"))
                splash.show()

                app.processEvents()

                app.setReach(result)

                session.initForms()

                session.mainForm.show()

                splash.finish(None)

                if session.tipsActive():
                    session.mainForm.ui.tips.raise_()

                sys.exit(app.exec_())

        else:
            formaAcceso.destroy()

